package com.bos.service.impl;

import com.bos.dao.repositories.RoleRepository;
import com.bos.dao.repositories.UserRepository;
import com.bos.domian.system.Role;
import com.bos.domian.system.User;
import com.bos.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.List;

/**
 * @author misterWei
 * @create 2018年12月23号:21点31分
 * @mailbox mynameisweiyan@gmail.com
 */
@Service
@Transactional(rollbackFor = {NullPointerException.class, ParseException.class,NumberFormatException.class})
public class UserServiceImpl implements UserService{
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private RoleRepository roleRepository;

    @Override
    public User findByUserName(String username) {
        return userRepository.findByUsername(username);
    }

    @Override
    public List<User> findAll() {
        return userRepository.findAll();
    }

    @Override
    public void save(User model, String[] roleIds) {
        if (roleIds != null) {
            for (String roleId : roleIds) {
                Role role = roleRepository.getOne(Integer.parseInt(roleId));
                model.getRoles().add(role);
            }
        }
        userRepository.save(model);
    }

}
